package controller

import (
	"github.com/gofiber/fiber"
	"gowork/onlineService/model"
	"strconv"
)

type OnlineServiceController struct {
	Answer *model.Answer
}

func (osc *OnlineServiceController)FillQuestionnaire(c *fiber.Ctx){

	osc.Answer = new(model.Answer)

	var answer model.Answer
	errs := c.BodyParser(&answer)
	if errs != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": errs.Error(),
		})
		return
	}
	res , err := osc.Answer.FillQuestionnaire(&answer)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": res["code"],
		"data": nil,
		"message": res["message"],
	})
	return
}

func (osc *OnlineServiceController)Query(c *fiber.Ctx){

	osc.Answer = new(model.Answer)

	customerIdStr := c.FormValue("customerId")
	customerId, err := strconv.ParseInt(customerIdStr, 10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	res , err := osc.Answer.Query(customerId)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": "200",
		"data": res,
		"message": "成功",
	})
	return
}

func (osc *OnlineServiceController)ExternalCallQuery(c *fiber.Ctx){

	osc.Answer = new(model.Answer)

	customerIdStr := c.FormValue("customerId")
	customerId, err := strconv.ParseInt(customerIdStr, 10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	res , err := osc.Answer.ExternalCallQuery(customerId)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": "200",
		"data": res,
		"message": "成功",
	})
	return
}























